package com.ruoyi.web.controller.archives;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.archives.domain.SelfCheckRecord;
import com.ruoyi.archives.service.ISelfCheckRecordService;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.pdf.PdfExporter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 自查记录Controller
 *
 * @author wng
 * @date 2024-08-04
 */
@RestController
@RequestMapping("/archives/selfCheckRecord")
public class SelfCheckRecordController extends BaseController {
    @Resource
    private ISelfCheckRecordService selfCheckRecordService;

    /**
     * 查询自查记录列表
     */
    @PreAuthorize("@ss.hasPermi('archives:selfCheckRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(SelfCheckRecord selfCheckRecord) {
        startPage();
        LoginUser loginUser = getLoginUser();
        if (loginUser.getUserId() != 1 && selfCheckRecord.getDeptId() == null) {
            selfCheckRecord.setDeptId(loginUser.getDeptId());
        }
        List<SelfCheckRecord> list = selfCheckRecordService.selectSelfCheckRecordList(selfCheckRecord);
        return getDataTable(list);
    }

    /**
     * 导出自查记录列表
     */
    @PreAuthorize("@ss.hasPermi('archives:selfCheckRecord:export')")
    @Log(title = "自查记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SelfCheckRecord selfCheckRecord) throws Exception{
        List<SelfCheckRecord> list = selfCheckRecordService.selectSelfCheckRecordList(selfCheckRecord);
//        ExcelUtil<SelfCheckRecord> util = new ExcelUtil<SelfCheckRecord>(SelfCheckRecord.class);
//        util.exportExcel(response, list, "自查记录数据");
        PdfExporter<SelfCheckRecord> util = new PdfExporter<>();
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=export.pdf");
        util.exportListToPdf(list,response.getOutputStream());
    }

    /**
     * 获取自查记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('archives:selfCheckRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(selfCheckRecordService.selectSelfCheckRecordById(id));
    }

    /**
     * 新增自查记录
     */
    @PreAuthorize("@ss.hasPermi('archives:selfCheckRecord:add')")
    @Log(title = "自查记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SelfCheckRecord selfCheckRecord) {
        LoginUser loginUser = getLoginUser();
        selfCheckRecord.setCreateBy(String.valueOf(loginUser.getUserId()));
        selfCheckRecord.setDeptId(loginUser.getDeptId());
        return toAjax(selfCheckRecordService.insertSelfCheckRecord(selfCheckRecord));
    }

    /**
     * 修改自查记录
     */
    @PreAuthorize("@ss.hasPermi('archives:selfCheckRecord:edit')")
    @Log(title = "自查记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SelfCheckRecord selfCheckRecord) {
        LoginUser loginUser = getLoginUser();
        selfCheckRecord.setUpdateBy(String.valueOf(loginUser.getUserId()));
        return toAjax(selfCheckRecordService.updateSelfCheckRecord(selfCheckRecord));
    }

    /**
     * 删除自查记录
     */
    @PreAuthorize("@ss.hasPermi('archives:selfCheckRecord:remove')")
    @Log(title = "自查记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(selfCheckRecordService.deleteSelfCheckRecordByIds(ids));
    }
}
